﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Compiled_Tests.Utils;
using LEET.CommonInterfaces.Automation;

namespace Compiled_Tests.AutomationAbstractions
{
    public class UIPath : IUIPath, IEnumerable<IUIPathElement>
    {
        public static UIPath Apply(String str)
        {
            UIPath path = new UIPath();
            String [] elements = str.Split(',');
            foreach(String element in elements)
                path.Add(PathElement.Apply(element));
            return path;
        }

        public void Add(IUIPathElement pathElement)
        {
            elements.Add(pathElement);
        }

        public void Add(String id, String name, String klass)
        {
            elements.Add(new PathElement(id, name, klass));
        }

        public UIPath()
        {
            elements = new List<IUIPathElement>();
        }

        public override string ToString()
        {
            StringBuilder buf = new StringBuilder();
            foreach (PathElement elem in elements)
                buf.Append(elem.ToString()).Append(", ");
            if(buf.Length > 0)
                buf.Remove(buf.Length - 2, 2);
            return buf.ToString();
        }

        private IList<IUIPathElement> elements;
        
        public IEnumerator<IUIPathElement> GetEnumerator()
        {
            return elements.GetEnumerator();
        }

        public IUIPathElement head { get {
            if (elements.Count() > 0)
                return elements[0];
            else
                return null;
        } }

        public IUIPath tail { get { 
            return new UIPathTail(1, this);  
        } }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    class UIPathTail : IUIPath
    {
        private int start;
        private UIPath original;
        public UIPathTail(int start, UIPath original)
        {
            this.start = start;
            this.original = original;
        }

        public IUIPathElement head
        {
            get {
                if (original.Count() > start)
                    return original.ElementAt(start);
                else
                    return null;
            }
        }

        public IUIPath tail
        {
            get {
                if (original.Count() > start)
                    return new UIPathTail(start + 1, original);
                else
                    return new UIPath() ;
            }
        }

        public override string ToString()
        {
            return head + ", " + tail.ToString();
        }


        public void Add(IUIPathElement elem)
        {
            original.Add(elem);
        }
    }


    public class PathElement : IUIPathElement
    {
        public static PathElement Apply(String element)
        {

            String id = "";
            String name = "";
            String klass = ""; 
            
            Match m = Regex.Match(element, " *\\(\"?(.*?)\"?; \"?(.*?)\"?; \"?(.*?)\"?\\)");
            if (m.Success)
            {
                id = m.Groups[1].Value;
                name = m.Groups[2].Value;
                klass = m.Groups[3].Value;
            }
            else 
                throw new InvalidIdentifierException();
            
            return new PathElement(id, name, klass);
        }

        public PathElement(String id, String name, String klass)
        {
            ID = id;
            Name = name;
            Class = klass;
        }

        public override string ToString()
        {
            return String.Format("({0}; {1}; {2})", ID, Name, Class);
        }

        public String ID { get; private set; }
        public String Name { get; private set; }
        public String Class { get; private set; }
    }
}
